package dep.service.impl;

import dep.dao.impl.UserDao;
import dep.dao.impl.UserDaoImpl;
import dep.domain.User;
import dep.util.MailUtils;
import dep.util.UuidUtil;

public class UserServiceImpl implements UserService{
    private UserDao userDao = new UserDaoImpl();
    /*
    * 用户注册
    * */
    @Override
    public boolean regist(User user) {
        //先查询用户名是否存在
        User u = userDao.findByUsername(user.getUsername());
        if(u!=null){
            //用户名已经存在，注册失败
            return false;
        }
        //设置激活码，唯一字符串
        user.setCode(UuidUtil.getUuid());
        //设置激活状态
        user.setStatus("N");
        userDao.Save(user);
        //发送激活邮件
        String content = "<a href='http://localhost/user/active?code="+user.getCode()+"'>点击激活【芝麻优购】</a>";
        MailUtils.sendMail(user.getEmail(),content,"激活邮件");

        //保存用户信息
        return true;
    }
    /*
    * 用户激活
    * */

    @Override
    public boolean active(String code) {
        //根据激活码查询用户对象
        User user = userDao.findByCode(code);
        if(user != null){
            userDao.updateStatus(user);
            return true;
        }else{
            return false;
        }
    }
    /*
    * 用户登陆
    * */

    @Override
    public User login(User user) {
        return userDao.findByUsernameAndPwd(user.getUsername(),user.getPassword());
    }
}
